package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.ShopCar;
import com.xiaomi.model.Result;
import com.xiaomi.service.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName ShoCarController
 * @Description TODO
 * @Author WL
 * @Date 2021/11/27 16:29
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/shopper", produces = "application/json; charset = utf-8")
public class ShoCarController {
    @Autowired
    private ShopCarService shopCarService;
    @GetMapping("/list.do")
    @ResponseBody
    public String listSearch(@RequestParam Integer userid) {
        List<ShopCar> lists = shopCarService.listSearch(userid);
        Result result = new Result();
        result.setCode(0);
        result.setData(lists);
        result.setMsg("成功");
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping("/insert.do")
    @ResponseBody
    public String insert(@RequestParam String name,
                         @RequestParam String iamg,
                         @RequestParam BigDecimal priced,
                         @RequestParam BigDecimal pricez,
                         @RequestParam String guige,
                         @RequestParam Integer quantity,
                         @RequestParam Integer sku_id) {
        shopCarService.insert(name, iamg, priced, pricez, guige, quantity, sku_id);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("成功");
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping("/update.do")
    @ResponseBody
    public String update(@RequestParam String guige,
                         @RequestParam Integer quantity,
                         @RequestParam Integer id) {
        shopCarService.update(guige, quantity, id);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("成功");
        String json = JSON.toJSONString(result);
        return json;
    }

    @GetMapping("/delete.do")
    @ResponseBody
    public String delete(@RequestParam Integer id) {
        shopCarService.delete(id);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("成功");
        String json = JSON.toJSONString(result);
        return json;
    }
}
